<?php

/**商城
 * Created by PhpStorm.
 * User: lh
 * Date: 2020/3/21
 * Time: 23:25
 */

class Controller_Pc_Api_V2_Malls extends Controller_Pc_Api_v2_Base
{

    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Malls
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Malls
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('malls', $this->request_params);

        // 服务
        $this->service = new Service_Api_Malls();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 筛选项
    public function action_query_condition()
    {
        try {
            $this->result['data'] = $this->service->query_condition();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取商品列表
    public function action_query()
    {
        try {
            $this->validation->query()->check();

            $this->result['data'] = $this->service->search($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取商品数据详情
    public function action_get_detail()
    {
        try {
            $this->validation->get_detail()->check();

            $this->result['data'] = $this->service->info($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取首页分类栏目
    public function action_get_index_classify()
    {
        try {
            $this->result['data'] = $this->service->get_index_classify();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取首页最新产品
    public function action_get_new_product()
    {
        try {
            $this->result['data'] = $this->service->get_new_product();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取首页热门
    public function action_get_hot_product()
    {
        try {
            $this->result['data'] = $this->service->get_hot_product();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取分类页面数据
    public function action_get_classify()
    {
        try {
            $this->result['data'] = $this->service->get_classify();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }


    // 获取购物车数量
    public function action_get_car_num()
    {
        try {
            $this->validation->get_car_num()->check();

            $this->result['data']['num'] = $this->service->get_car_num($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 加入购物车
    public function action_add_car()
    {
        $db = Database::instance();
        $db->begin();
        try {
            $this->validation->add_car()->check();

            $this->result['data'] = $this->service->add_car($this->mid, $this->request_params);
            $db->commit();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
            $db->rollback();
        }
    }

    // 获取购物车列表
    public function action_get_car_list()
    {
        try {
            $this->validation->get_car_list()->check();

            $this->result['data'] = $this->service->get_car_list($this->mid);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 删除购物车数据
    public function action_del_car()
    {
        try {
            $this->validation->del_car()->check();

            $this->result['data'] = $this->service->del_car($this->mid, $this->request_params['car_id']);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 更换购物车商品
    public function action_change_car_list()
    {
        try {
            $this->validation->change_car_list()->check();

            $this->result['data'] = $this->service->change_car_list($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 计算运费
    public function action_get_express_fee()
    {
        try {
            $this->validation->get_express_fee()->check();

            $this->result['data'] = $this->service->get_express_fee($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 创建订单
    public function action_add_order()
    {
        try {
            $this->validation->add_order()->check();

            $this->request_params['member_info'] = $this->member_info;
            $this->result['data'] = $this->service->create_order($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 订单详情
    public function action_get_order_detail()
    {
        try {
            $this->validation->get_order_detail()->check();

            $this->result['data'] = $this->service->get_order_detail($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }
}